$(function(){
	
	//增加减少按钮的触发事件
	$('span[name="changeChooseNum"]').on("click",function(){
		var className =  $(this).attr("class");
		var currentChooseNum = parseInt($("#chooseNum").attr("value"));
		var productItemStartingBatch = parseInt($("#productItemStartingBatch").attr("value"));
		if(className=='add'){
			currentChooseNum = currentChooseNum + 1;
			$("#chooseNum").attr("value",currentChooseNum);
			$("#chooseNum").val(currentChooseNum);	
		}else{
			currentChooseNum = currentChooseNum - 1;
			if(currentChooseNum<=productItemStartingBatch){
				$("#chooseNum").attr("value",productItemStartingBatch);
				$("#chooseNum").val(productItemStartingBatch);
			}else{
				$("#chooseNum").attr("value",currentChooseNum);
				$("#chooseNum").val(currentChooseNum);
			}
			
		}	
	});
	
	//提交给服务端再次检验购买数量是否合法,如果合法，则生成购物车项
	function sendChooseNum(chooseProductItemId,currentChooseNum,productId){
		var productItemStartingBatch = $("#productItemStartingBatch").val();
		$.ajax({
			type:"get",
			url:base_url+"customer/judgeDiscountProductChooseNum",
			data:{"chooseProductItemId":chooseProductItemId,"currentChooseNum":currentChooseNum,"productId":productId},
			async:false,
			success:function(data){
				if(data.code=='0'){
					tip.content("库存不足！").show();
					$("#productItemStock").text(data.data.stock);
					$('#'+chooseProductItemId).attr("name",data.data.stock);
					$("#chooseNum").attr("value",data.data.stock);
					$("#chooseNum").val(data.data.stock);
					$("#canGoShopCart").attr("value","false");
				}else if(data.code=='1'){
					tip.content("所买小于起批量!").show();
						$("#productItemStock").text(data.data.stock);
						$('input[name="'+chooseProductItemId+'"]').attr("class",data.data.startingBatch);
						$("#chooseNum").attr("value",data.data.startingBatch);
						$("#chooseNum").val(data.data.startingBatch);
					$("#canGoShopCart").attr("value","false");
				}else if(data.code=='2'){
					tip.content("所选购买数量必须为阿拉伯数字！").show();
					$("#chooseNum").attr("value",productItemStartingBatch);
					$("#chooseNum").val(productItemStartingBatch);
					$("#canGoShopCart").attr("value","false");
				}else if(data.code=='3'){
					tip.content("所选购买数量不能位空！").show();
					$("#chooseNum").attr("value",productItemStartingBatch);
					$("#chooseNum").val(productItemStartingBatch);
					$("#canGoShopCart").attr("value","false");
				}else if(data.code=='4'){
					$("#canGoShopCart").attr("value","true");
				}else{
					tip.content("你所选择的规格不存在").show();
					var json = {
							"productId":productId
					}
					var jsonStr = JSON.stringify(json);
					window.location.href=base_url+"customer/getDiscountProductDetail?jsonStr="+jsonStr;
				}
			},
			error:function(){
				tip.content("连接失败").show();
			}
		
		});
	}
	//客户端校验选择的购买数量是否合法
	function judgeBuyNum(){
		var currentChooseNum = $("#chooseNum").val().replace(/\s+/g,"");
		var productItemStartingBatch = $("#productItemStartingBatch").val();
		var productItemStock = $("#productItemStock").val();
		var chooseProductItemId = $("#chooseProductItemId").val();
		var productId = $("#productId").attr("value");
		if(currentChooseNum!=""){
			if(isNaN(currentChooseNum)){
				tip.content("购买数量必须为阿拉伯数字").show();
				$("#chooseNum").val(productItemStartingBatch);
				$("#chooseNum").attr("value",productItemStartingBatch);
				$("#canGoShopCart").attr("value","false");
			}else{
				if(parseInt(currentChooseNum)<parseInt(productItemStartingBatch)){
					tip.content("选择的购买数量不能小于起批量").show();
					$("#chooseNum").attr("value",productItemStartingBatch);
					$("#chooseNum").val(productItemStartingBatch);
					$("#canGoShopCart").attr("value","false");
				}else{
					if(parseInt(currentChooseNum)>parseInt(productItemStock)){
						tip.content("库存不足").show();
						$("#chooseNum").attr("value",productItemStock);
						$("#chooseNum").val(productItemStock);
						$("#canGoShopCart").attr("value","false");
					}else{
						sendChooseNum(chooseProductItemId,currentChooseNum,productId);
					}
					
				}
			}
		}else{
			tip.content("购买数量不能为空").show();
			$("#chooseNum").val(productItemStartingBatch);
			$("#chooseNum").attr("value",productItemStartingBatch);
			$("#canGoShopCart").attr("value","false");
		}
	}

	//点击切换规格
	$(".type").on("click",function(){
		var buttonId = $(this).attr("id");
		var platformPrice = $('input[name="'+buttonId+'"]').attr("data-platformPrice");
		var marketPrice = $('input[name="'+buttonId+'"]').attr("data-marketPrice");
		var startingBatch = $('input[name="'+buttonId+'"]').attr("class");
		var stock = $(this).attr("name");
		//设置显示的价钱和库存
		var html = "";
		$("#priceContent").empty();
		if(parseFloat(platformPrice)<parseFloat(marketPrice)){
			html = html + '<span class="price" id="platformPrice" >'+platformPrice+'</span> <del id="marketPrice">'+marketPrice+'元</del>';
		}else{
			html = html + '<span class="price" id="platformPrice" >'+platformPrice+'</span>';
		}
		html = html + '<span class="item_amount floatRight"><span>剩余<span class="amount" id="productItemStock" >'+stock+'</span>件</span></span>';
		$("#priceContent").append(html);
		//设置显示的库存
		$("#productItemStock").text(stock);
		//设置起批量
		$("#productItemStartingBatch").attr("value",startingBatch);
		$("#chooseNum").attr("value",startingBatch);
		$("#chooseNum").val(startingBatch);
		$(".type").removeClass("active");
		$(this).addClass("active");
		$("#chooseProductItemId").attr("value",buttonId);
	});
	
	//立即剁手
	$(".item_buy").on("click",function(){
		judgeBuyNum();
		var canGoShopCart = $("#canGoShopCart").val();
		var productId = $("#productId").val();
		var productItemId = $("#chooseProductItemId").val();
		var amount = $("#chooseNum").val();
		if(canGoShopCart=='true'){
			var json = {
					"amount":amount,
					"productId":productId,
					"productItemId":productItemId
			}
			var jsonStr = JSON.stringify(json);
			window.location.href = base_url + "customer/buyDiscountProduct?jsonStr="+jsonStr;
		}
	});
	
});
	
	//进店逛逛
	$(".goToShop").click(function(){
		var sellerId = $(this).attr("data-sellerId");
		var json = {
				"sellerId":sellerId
		}
		var jsonStr = JSON.stringify(json);
		window.location.href = base_url + "customer/getShopIndexMsg?jsonStr="+jsonStr;
	});
	
	FormatImg($(".format"),$(".format").parents(".swiper-container"));
//加载图片
window.onscroll = function(){
	lazyLoad($(".lazy[data-load='false']"));
}
